﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using Microsoft.Win32;
using System.IO;

// Set 'RunInstaller' attribute to true.
[RunInstaller(true)]
public class MyInstallerClass : Installer
{
    public static void Main(String[] args)
    {
    }

    //installContext //installContext;

    public MyInstallerClass()
    {
    }

    public override void Install(IDictionary savedState)
    {
        //installContext = new //installContext(
                   //Path.Combine(this.Context.Parameters["InstallPath"], "MPITemplateInstall.log"), null);


        base.Install(savedState);

        savedState.Remove("InstalledTemplate");
        string installPath = this.Context.Parameters["InstallPath"];
        if (installPath.EndsWith("\\\\"))
        {
            installPath = installPath.Substring(0, installPath.Length - 2);
        }

        // Get the VS2010 Path
        RegistryKey registry = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0");
        if (registry == null)
        {
            //installContext.LogMessage("Could not find Visual Studio 2010 installation.");
            return;
        }
        string vsPath = registry.GetValue("InstallDir") as string;

        //installContext.LogMessage("Installing into: " + vsPath);

        // Make sure the icon file exists.
        installPath = Path.Combine(installPath, "Template");
        string iconPath = Path.Combine(installPath, "MPI Project.ico");
        if (!File.Exists(iconPath))
        {
            //installContext.LogMessage("Unable to install template - icon not found.");
            return;
        }

        // Converts from vsPath ~ C:\Program Files\Microsoft Visual Studio 9.0\COMMON7\IDE
        // to vcProjects ~ C:\Program Files\Microsoft Visual Studio 9.0\VC\vcprojects
        string vcProjects = Path.Combine(Path.Combine(new DirectoryInfo(vsPath).Parent.Parent.FullName, "VC"), "vcprojects");
        if (!Directory.Exists(vcProjects))
        {
            //installContext.LogMessage("Unable to install template - vcprojects path not found.");
            return;
        }

        // Make sure the files don't already exist.
        if (File.Exists(Path.Combine(vcProjects, "MPI Project.vsz")) ||
            File.Exists(Path.Combine(vcProjects, "MPI Project.vsdir")) ||
            File.Exists(Path.Combine(vcProjects, "MPI Project.ico")))
        {
            //installContext.LogMessage("Unable to install template - it is already installed.");
            return;
        }

        try
        {
            using (StreamWriter SW = new StreamWriter(Path.Combine(vcProjects, "MPI Project.vsz")))
            {
                SW.WriteLine("VSWIZARD 7.0");
                SW.WriteLine("Wizard=VsWizard.VsWizardEngine.9.0");
                SW.WriteLine("Param=\"WIZARD_NAME = MPI Project\"");
                SW.WriteLine(string.Format("Param=\"ABSOLUTE_PATH = {0}\"", installPath));
                SW.WriteLine("Param=\"CONSOLE_TYPE_ONLY = true\"");
                SW.WriteLine("Param=\"WIZARD_ID = 75\"");
            }
        }
        catch (Exception e)
        {
            //installContext.LogMessage("Unable to install template - " + e.Message);
            return;
        }

        try
        {
            using (StreamWriter SW = new StreamWriter(Path.Combine(vcProjects, "MPI Project.vsdir")))
            {
                SW.WriteLine("MPI Project.vsz| |MPI Project|1|This wizard sets up a project that can be used as the starting point of developing an MPI application.| |6777| |MPI Project");
            }
        }
        catch (Exception e)
        {
            try
            {
                File.Delete(Path.Combine(vcProjects, "MPI Project.vsz"));
            }
            catch { }
            //installContext.LogMessage("Unable to install template - " + e.Message);
            return;
        }

        try
        {
            File.Copy(iconPath, Path.Combine(vcProjects, "MPI Project.ico"), true);
        }
        catch (Exception e)
        {
            try
            {
                File.Delete(Path.Combine(vcProjects, "MPI Project.vsz"));
                File.Delete(Path.Combine(vcProjects, "MPI Project.vsdir"));
            }
            catch { }
            //installContext.LogMessage("Unable to install template - " + e.Message);
            return;
        }

        savedState.Add("InstalledTemplate", true);
    }

    public override void Uninstall(IDictionary savedState)
    {
        //installContext = new //installContext(
                   //Path.Combine(this.Context.Parameters["InstallPath"], "MPITemplateInstall.log"), null);

        base.Uninstall(savedState);

        // If it wasn't installed during the installation, then don't remove.
        if (!savedState.Contains("InstalledTemplate"))
        {
            return;
        }

        // Get the VS2010 Path
        RegistryKey registry = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0");
        if (registry == null)
        {
            //installContext.LogMessage("Could not find Visual Studio 2010 installation.");
            return;
        }
        string vsPath = registry.GetValue("InstallDir") as string;

        //installContext.LogMessage("Removing from: " + vsPath);

        // Converts from vsPath ~ C:\Program Files\Microsoft Visual Studio 9.0\COMMON7\IDE
        // to vcProjects ~ C:\Program Files\Microsoft Visual Studio 9.0\VC\vcprojects
        string vcProjects = Path.Combine(Path.Combine(new DirectoryInfo(vsPath).Parent.Parent.FullName, "VC"), "vcprojects");
        if (!Directory.Exists(vcProjects))
        {
            //installContext.LogMessage("Unable to remove template - path not found.");
            return;
        }

        bool errors = false;
        try
        {
            File.Delete(Path.Combine(vcProjects, "MPI Project.ico"));
        }
        catch (Exception e)
        {
            errors = true;
            //installContext.LogMessage("Unable to remove template - " + e.Message);
        }

        try
        {
            File.Delete(Path.Combine(vcProjects, "MPI Project.vsz"));
        }
        catch (Exception e)
        {
            errors = true;
            //installContext.LogMessage("Unable to remove template - " + e.Message);
        }

        try
        {
            File.Delete(Path.Combine(vcProjects, "MPI Project.vsdir"));
        }
        catch (Exception e)
        {
            errors = true;
            //installContext.LogMessage("Unable to remove template - " + e.Message);
        }

        if (!errors)
            savedState.Remove("InstalledTemplate");

        //installContext.c
    }
}
